import {messages} from "share/common";
import moment from 'moment';
import checkCenterService from "./check-center.service";
import {message} from "antd";
import FileSaver from "file-saver";

const common = {

  getRadioMap(value) {
    const maps = [
      {label: messages('financial-management.key172'/*待确认*/), value: '2'},
      {label: messages('financial-management.key37'/*已确认*/), value: '1'},
      {label: messages('financial-management.key36'/*全部*/), value: ''}
    ];
    if (value === '') {
      return messages('financial-management.key36'/*全部*/);
    }
    if (value === '1') {
      return messages('financial-management.key37'/*已确认*/);
    }
    if (value === '2') {
      return messages('financial-management.key172'/*待确认*/);
    }
    return maps;
  },

  searchParams: {
    checkBatchNo: '',
    channel: 'hly-admin',
    companyOID: '',
    endTime: '',
    journeyId: '',
    journeyType: '',
    orderType: '',
    page: 0,
    size: 20,
    startTime: '',
    vendorName: '',
    recordId: '',
    reconciliationStatus: '',
    takeOffStartTime: '',
    takeOffEndTime: ''
  },

  searchParamsCar: {
    channel: 'hly-admin',
    companyOID: '',
    endTime: '',
    payType: '',
    orderNo: '',
    page: 0,
    size: 20,
    startTime: '',
    vendorName: '',
    reconciliationStatus: '',
  },

  searchParamsYget: {
    channel: 'hly-admin',
    companyOID: '',
    endTime: '',
    orderType: '',
    orderNo: '',
    orderCreateStartTime: '',
    orderCreateEndTime: '',
    page: 0,
    size: 20,
    startTime: '',
    vendorName: '',
    reconciliationStatus: '',
  },

  searchParamsOrder: {
    channel: "hly-admin",
    companyOID: "",
    payType: "",// 付款类型
    orderNo: "",// 订单号
    page: 0,
    size: 20,
    journeyId: '',
    vendorName: "",// 消费商名称
    reconciliationStatus: "", // 对账状态
    orderDateStart: "",
    orderDateEnd: "",
    isArtInvoice: ""// 费用状态
  },

  getSearchParams(type) {
    switch (type) {
      case 'yget':
        this.searchParamsYget.orderCreateStartTime = this.getNextMonth('', -3);
        this.searchParamsYget.orderCreateEndTime = this.getNextMonth('', 0);
        return JSON.parse(JSON.stringify(this.searchParamsYget));
      case 'car':
        this.searchParamsCar.payStartTime = this.getNextMonth('', -3);
        this.searchParamsCar.payEndTime = this.getNextMonth('', 0);
        return JSON.parse(JSON.stringify(this.searchParamsCar));
      case 'dining':
        this.searchParamsOrder.orderDateStart = this.getNextMonth('', -3);
        this.searchParamsOrder.orderDateEnd = this.getNextMonth('', 0);
        return JSON.parse(JSON.stringify(this.searchParamsOrder));
      default:
        // this.searchParams.endTime = this.getNextMonth('', 0);
        // this.searchParams.startTime = this.getNextMonth('', -3);
        return JSON.parse(JSON.stringify(this.searchParams));
    }
  },

  getNextMonth(date, num, isMoment) {
    const momentDate = moment(date || new Date());
    momentDate.add(num, 'M');
    if (isMoment) {
      return momentDate;
    } else {
      return momentDate.format('YYYY-MM-DD');
    }
  },

  getNextYear(date) {
    let arr = date.split('-');
    let year = arr[0]; //获取当前日期的年份
    let month = arr[1]; //获取当前日期的月份
    let day = arr[2]; //获取当前日期的日
    let nextYear = parseInt(year) - 1;
    return nextYear + '-' + month + '-' + day
  },

  //数字每三位中加一个‘，’
  numberToString(num) {
    let result = [];
    for (let i = 0; i < num.toString().length; i++) {
      let b = i + 1;
      result.push(num.toString()[i])
      if (b % 3 == 0 && i != 0) {
        result.push(',');
      }
    }
    let str = "";
    result.map((item) => {
      str += item
    });
    return str
  },

  downloadResultFile(key) {
    let hide = message.loading(`${messages('financial-management.key685')/*正在下载*/}...`);
    checkCenterService.downloadResultFile(key).then(res => {
      if (res.data.exportFileRecord.tempUrl) {
        window.open(res.data.exportFileRecord.tempUrl);
      }
      hide();
    })
  },

  addIndexNumber(result, pagination) {
    if (result) {
      result.map((item, index) => {
        item.index = (pagination.current - 1) * pagination.pageSize + index + 1;
      })
    }
  },

  downloadTemplateFile(key) {
    let hide = message.loading(`${messages('financial-management.key685')/*正在下载*/}...`);
    checkCenterService.downloadTemplateFile(key).then(res => {
      if (res.data.exportFileRecord.tempUrl) {
        window.open(res.data.exportFileRecord.tempUrl);
      }
      hide();
    })
  },

  formatCurrency(num) {
    if (num) {
      num = num.toString().replace(/\$|\,/g, '');
    }
    if (isNaN(num))
      num = "0";
    let sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    let cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
      cents = "0" + cents;
    for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
      num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
  }

};
export default common
